/*AyflEomXWyqCHfdTxzWda1e2+xM16UdiCeHmo0c2GDI=*/
/*
 * @Descripttion:
 * @version:
 * @Author: Chendch
 * @Date: 2020-10-14 10:01:59
 * @LastEditors: Chendch
 * @LastEditTime: 2021-09-28 15:43:50
 */
import {ajax, base, pageTo} from "nc-lightapp-front";
const {NCTooltip, NCButton, NCHotKeys, NCPopover} = base;

function checkAddButtons(params) {
  const { addButtons = [], hasAddButton=false,refName="" } = params;
  if(hasAddButton && addButtons && Array.isArray(addButtons)) {
    let buttonsMap = {};
    ajax({
      url: '/nccloud/platform/pub/refadd.do',
      data: {
        appcodes: addButtons.map(button => {
            let { appcode } = button;
            buttonsMap[appcode] = button;
            return appcode;
        }),
      },
      success: (res) => {
        const {success, data} = res;
        if(!success) return;
        this.setState({
          addButtonList: data.AppRegisterVOs.map((item) => {
            return {...buttonsMap[item.code], name: item.name};
          }, () => {
            console.log("addButtonList--", addButtonList)
          })
        })
      }
    })
  }
}

function renderAddButtons(params = "") {
  const {addButtonList = [], isAddButtonShow, currentButtonIndex, selectedValues} = this.state;
  if(!addButtonList) return null;
  let additionParams = {};
  if(selectedValues.size) {
    for(let [k,v] of selectedValues) {
      additionParams[k] = v;
    }
  }
  console.log("addButtonList--", addButtonList)
  switch(addButtonList.length) {
    case 0:
      return null;
    case 1:
      return (
        <NCTooltip
          placement="top"
          inverse
          overlay={`${this.state.jsonInsideRefer["containers-refer-0046"]}  (${NCHotKeys.USUAL_KEYS
              .ADD})`}
          trigger={['hover', 'focus']}
          className="model-helper-overlay"
        >
          <NCButton onClick={() => {
            let { appcode, pagecode } = addButtonList[0];
            pageTo.openTo(null, {
                appcode,
                pagecode,
                status: 'browse',
                params: JSON.stringify({...additionParams, ...params})
            })
          }}>
              {this.state.jsonInsideRefer["containers-refer-0046"]}
            (<span className="text-decoration-underline">/</span>)
          </NCButton>
        </NCTooltip>
      );
    default:
      let length = addButtonList.length;
      return (
        <NCPopover
          container={this.popWindow}
          className="popover-in-refer add"
          trigger="click"
          placement="bottom"
          show={isAddButtonShow}
	        onVisibleChange={isAddButtonShow => {
	            this.setState({ isAddButtonShow })
	        }}
          content={<div className="add-button-wrapper">{addButtonList.map(({ name, appcode, pagecode }, i) => {
            return <div className={`add-button-item nc-theme-btn-secondary ${(currentButtonIndex%length + length)%length === i ? 'selected' : ''}`} key={name} onClick={() => {
                pageTo.openTo(null, {
                  appcode,
                  pagecode,
                  status: 'browse',
                  params: JSON.stringify({...additionParams, ...params})
                })
            }}>{name}</div>;
          })}</div>}
          fieldid="more_btn_menu"
	        show={isAddButtonShow}
	        onVisibleChange={isAddButtonShow => {
	          this.setState({ isAddButtonShow })
	        }}
        >
          <NCTooltip
            placement="top"
            inverse
            overlay={`${this.state.jsonInsideRefer["containers-refer-0046"]}  (${NCHotKeys.USUAL_KEYS
                .ADD})`}
            trigger={['hover', 'focus']}
            className="model-helper-overlay"
          >
            <NCButton onClick={() => { this.setState(({ isAddButtonShow }) => ({ isAddButtonShow: !isAddButtonShow })) }}>
              {this.state.jsonInsideRefer["containers-refer-0046"]}
                (<span className="text-decoration-underline">/</span>)
                <i className="iconfont icon-hangcaozuoxiala1"></i>
            </NCButton>
          </NCTooltip>
        </NCPopover>
      )
  }
}

export {
  checkAddButtons,
  renderAddButtons
}
/*AyflEomXWyqCHfdTxzWda1e2+xM16UdiCeHmo0c2GDI=*/
